#include "common.h"
#include "main.h"
#include "functions.h"


VOID funvtion_TCHAR_to_CHAR (TCHAR * src, CHAR * tar, LONG MaxLength) {
	if (sizeof(TCHAR) == sizeof(CHAR)) strcpy_s(tar, MaxLength, (CHAR *) src);
	else WideCharToMultiByte(CP_ACP, 0, (WCHAR *)src, -1, tar, MaxLength, NULL, FALSE);
}
VOID funvtion_CHAR_to_TCHAR (CHAR * src, TCHAR * tar, LONG MaxLength) {
	if (sizeof(TCHAR) == sizeof(CHAR)) strcpy_s((CHAR *) tar, MaxLength, src);
	else MultiByteToWideChar(CP_ACP, 0, src, -1, (WCHAR *)tar, MaxLength);
}


VOID funvtion_ConvertConfigInfoToClientInfo(SRUNX_CONFIG_INFO * config, SRUNX_CLIENT_INFO * client) {
	funvtion_TCHAR_to_CHAR(config->username, client->username, LENGTH_STRING_MAX_CONFIG); 
	funvtion_TCHAR_to_CHAR(config->password, client->password, LENGTH_STRING_MAX_CONFIG); 
	if (config->Mode > 0) sprintf_s(client->mode, 3, "0\0");
	else sprintf_s(client->mode, 3, "1\0");
	return;
}

VOID funvtion_CheckDlgButton(HWND hWnd, UINT idc, BOOL check) {
	if (check) CheckDlgButton(hWnd, idc, BST_CHECKED);
	else CheckDlgButton(hWnd, idc, BST_UNCHECKED);
}

VOID action_ModifyUIbyConfigInfo (SRUNX_CONFIG_INFO * config, HWND hWnd) {
	SendMessage( GetDlgItem(hWnd, IDC_COMBO_MODE), (UINT) CB_SETCURSEL, (WPARAM)config->Mode, (LPARAM) NULL);
	funvtion_CheckDlgButton (hWnd, IDC_CHECK_SAVEUN, config->UIConfig & DWORD_CONFIG_SAVEUN);
	funvtion_CheckDlgButton (hWnd, IDC_CHECK_SAVEPW, config->UIConfig & DWORD_CONFIG_SAVEPW);
	funvtion_CheckDlgButton (hWnd, IDC_CHECK_LOGAUT, config->UIConfig & DWORD_CONFIG_LOGAUT);
	funvtion_CheckDlgButton (hWnd, IDC_CHECK_FOURCE, config->UIConfig & DWORD_CONFIG_FOURCE);
	funvtion_CheckDlgButton (hWnd, IDC_CHECK_RELINK, config->UIConfig & DWORD_CONFIG_RELINK);
	SetDlgItemText(hWnd, IDC_EDIT_USERNAME, config->username);
	SetDlgItemText(hWnd, IDC_EDIT_PASSWORD, config->password);
}

VOID action_ModifyConfigInfobyUI (SRUNX_CONFIG_INFO * config, HWND hWnd) {
	config->Mode = SendMessage( GetDlgItem(hWnd, IDC_COMBO_MODE), (UINT) CB_GETCURSEL, (WPARAM) NULL, (LPARAM) NULL);
	if( IsDlgButtonChecked (hWnd, IDC_CHECK_SAVEUN)) config->UIConfig |= DWORD_CONFIG_SAVEUN;
	else config->UIConfig &= ~DWORD_CONFIG_SAVEUN;
	if( IsDlgButtonChecked (hWnd, IDC_CHECK_SAVEPW)) config->UIConfig |= DWORD_CONFIG_SAVEPW;
	else config->UIConfig &= ~DWORD_CONFIG_SAVEPW;
	if( IsDlgButtonChecked (hWnd, IDC_CHECK_LOGAUT)) config->UIConfig |= DWORD_CONFIG_LOGAUT;
	else config->UIConfig &= ~DWORD_CONFIG_LOGAUT;
	if( IsDlgButtonChecked (hWnd, IDC_CHECK_FOURCE)) config->UIConfig |= DWORD_CONFIG_FOURCE;
	else config->UIConfig &= ~DWORD_CONFIG_FOURCE;
	if( IsDlgButtonChecked (hWnd, IDC_CHECK_RELINK)) config->UIConfig |= DWORD_CONFIG_RELINK;
	else config->UIConfig &= ~DWORD_CONFIG_RELINK;

	GetDlgItemText(hWnd, IDC_EDIT_USERNAME, config->username, LENGTH_STRING_MAX_CONFIG);
	GetDlgItemText(hWnd, IDC_EDIT_PASSWORD, config->password, LENGTH_STRING_MAX_CONFIG);
}


VOID action_ModifyUIbyState(UINT state, HWND hWnd) {
	switch (state) {
		default: SendMessage( GetDlgItem(hWnd, IDC_BUTTON_LOGIN), (UINT) WM_SETTEXT, (WPARAM) NULL, (LPARAM) STRING_UI_BUTTON_LOG_0); break;
		case 1 : SendMessage( GetDlgItem(hWnd, IDC_BUTTON_LOGIN), (UINT) WM_SETTEXT, (WPARAM) NULL, (LPARAM) STRING_UI_BUTTON_LOG_1); break;
		case 2 : SendMessage( GetDlgItem(hWnd, IDC_BUTTON_LOGIN), (UINT) WM_SETTEXT, (WPARAM) NULL, (LPARAM) STRING_UI_BUTTON_LOG_2); break;
		case 3 : SendMessage( GetDlgItem(hWnd, IDC_BUTTON_LOGIN), (UINT) WM_SETTEXT, (WPARAM) NULL, (LPARAM) STRING_UI_BUTTON_LOG_3); break;
	}
}

VOID action_InitNotifyTray(HWND hWnd) {
	NotifyTray.cbSize = sizeof(NOTIFYICONDATA);
	NotifyTray.hWnd = hWnd;
	NotifyTray.uFlags = NIF_INFO |NIF_MESSAGE | NIF_ICON | NIF_TIP; 
	NotifyTray.uID = 21;
	NotifyTray.uCallbackMessage = WM_NOTIFYICON;
	NotifyTray.hIcon = hIcon_T_b;
	NotifyTray.dwInfoFlags = NIIF_INFO;
	NotifyTray.uVersion = NOTIFYICON_VERSION;
	NotifyTray.uTimeout=1000;
	NotifyTray.dwState=NIS_SHAREDICON;
	NotifyTray.dwStateMask=0;
	NotifyTray.dwInfoFlags=NIIF_INFO;
	Shell_NotifyIcon(NIM_ADD, &NotifyTray);
	return;
}

VOID action_InitBeforeUI() {

	hIcon_T_w = LoadIcon(hInst, MAKEINTRESOURCE (IDI_ICON_STATE_W));
	hIcon_T_b = LoadIcon(hInst, MAKEINTRESOURCE (IDI_ICON_STATE_B));
	hIcon_T_r = LoadIcon(hInst, MAKEINTRESOURCE (IDI_ICON_STATE_R));
	hIcon_T_o = LoadIcon(hInst, MAKEINTRESOURCE (IDI_ICON_STATE_O));
	hIcon_T_g = LoadIcon(hInst, MAKEINTRESOURCE (IDI_ICON_STATE_G));
	hIcon_S = LoadIcon(hInst, MAKEINTRESOURCE (IDI_ICON_SRUNX_S));
	hMenuTray = LoadMenu(hInst, MAKEINTRESOURCE (IDR_MENU_TRAY));
	client = new module_client();
	configure = new module_configure();
	configure->getConfigInfo(&config_info);
}
VOID action_InitAfterUI() {
	action_InitNotifyTray(hWndMain);
}

VOID action_SaveConfig() {
	action_ModifyConfigInfobyUI(&config_info, hWndMain);
	configure->setConfigInfo(&config_info);
	configure->saveConfigInfo();
}